﻿
//function createHistoryMapWindow(txttitle, toolbar) {
//    var tmpwin;
//    if (meimaptype == 0) {
//        if (toolbar) {
//            tmpwin = new Ext.ux.GMapWindow({
//                title: txttitle,
//                closable: true,
//                width: 500,
//                height: 450,
//                minimizable: false,
//                maximizable: true,
//                plain: true,
//                x: nextwindowx, //defined in paneldevicelist.js
//                y: nextwindowy,
//                tbar: toolbar
//            });
//        }
//        else {
//            tmpwin = new Ext.ux.GMapWindow({
//                title: txttitle,
//                closable: true,
//                width: 500,
//                height: 450,
//                minimizable: false,
//                maximizable: true,
//                plain: true,
//                x: nextwindowx, //defined in paneldevicelist.js
//                y: nextwindowy
//            });
//        }
//    }
//    else if (meimaptype == 1) {
//        if (toolbar) {
//            tmpwin = new Ext.ux.BMapWindow({
//                title: txttitle,
//                closable: true,
//                width: 500,
//                height: 450,
//                minimizable: false,
//                maximizable: true,
//                plain: true,
//                x: nextwindowx,
//                y: nextwindowy,
//                tbar: toolbar
//            });
//        }
//        else {
//            tmpwin = new Ext.ux.BMapWindow({
//                title: txttitle,
//                closable: true,
//                width: 500,
//                height: 450,
//                minimizable: false,
//                maximizable: true,
//                plain: true,
//                x: nextwindowx,
//                y: nextwindowy
//            });
//        }
//    }
//    else if (meimaptype == 2) {
//        if (toolbar) {
//            tmpwin = new Ext.ux.MMapWindow({
//                title: txttitle,
//                closable: true,
//                width: 500,
//                height: 450,
//                minimizable: false,
//                maximizable: true,
//                plain: true,
//                x: nextwindowx,
//                y: nextwindowy,
//                tbar: toolbar
//            });
//        }
//        else {
//            tmpwin = new Ext.ux.MMapWindow({
//                title: txttitle,
//                closable: true,
//                width: 500,
//                height: 450,
//                minimizable: false,
//                maximizable: true,
//                plain: true,
//                x: nextwindowx,
//                y: nextwindowy
//            });
//        }
//    }
//    return tmpwin;
//}

function smsReportWindow(isLoadOneDayMessage) {
    var pagesize = 15;
    var day = new Date();
    var day = new Date();
    var today = day.getFullYear() + '-' + padLeft((day.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(day.getDate().toString(), "0", 2);
    var yyday = addDays(day, -1);
    var yestoday = yyday.getFullYear() + '-' + padLeft((yyday.getMonth() + 1).toString(), "0", 2) + '-' + padLeft(yyday.getDate().toString(), "0", 2);
    var trackerstore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: 'HandlerSearchTrackerName',
            timeout: 240000
        }),
        reader: new Ext.data.JsonReader({
            fields: [{ name: 'trackname' }, { name: 'snid'}],
            root: "data"
        }),
        autoLoad: true,
        listeners: {
            'load': function() {
                if (isLoadOneDayMessage == true) {
                    var ids = "";
                    for (var i = 0; i < trackerstore.getCount(); i++) {
                        var record = trackerstore.getAt(i);
                        if (ids == "")
                            ids = record.get('snid');
                        else
                            ids += "," + record.get('snid');
                    }
                    ids = trackNameCombo.getValue() == "" ? ids : trackNameCombo.getValue();
                    smsReportStore.reload({
                        params: {
                            start: 0,
                            limit: 15,
                            ids: ids,
                            fromtime: today,
                            totime: today + " " + nowtime,
                            inout: -1
                        }
                    });
                    isLoadOneDayMessage == false;
                }
            }
        }
    });

    var trackNameCombo = new Ext.ux.form.CheckboxCombo({
        name: 'tracker',
        store: trackerstore,
        mode: 'local',
        displayField: 'trackname',
        width: 130,
        valueField: 'snid',
        hiddenName: 'tracker',
        enableKeyEvents: true,
        editable: true,
        autoSelect:true
    });

    trackNameCombo.on('keyup', function() {
        trackerstore.filter('trackname', trackNameCombo.getValue(), true, false);
        trackNameCombo.bindStore(trackerstore);
    });

    trackNameCombo.on('blur', function() {
        trackerstore.clearFilter();
        trackNameCombo.bindStore(trackerstore);
    });

    var fromDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        value: today
    });


    var fromTimeEditor = new Ext.form.TimeField({
        value:"00:00",
        format: 'H:i',
        width: 70
    });

    var toDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        value: today
    });

    var toTimeEditor = new Ext.form.TimeField({
        value:'23:59',
        format: 'H:i',
        width: 70
    });

    var inOutSMSCombobox = new Ext.form.ComboBox({
        editable: false,
        disableKeyFilter: true,
        typeAhead: false,
        mode: 'local',
        forceSelection: true,
        triggerAction: 'all',
        store: new Ext.data.ArrayStore({
            fields: ['id', 'text'],
            data: [[-1, _SMSResource_Received + "/" + _SMSResource_Send], [1, _SMSResource_Receive], [0, _SMSResource_Send]]
        }),
        displayField: 'text',
        width: 100,
        valueField: 'id'
    });

    var tbar = new Ext.Toolbar({
        items: [_Alarm_TrackerName + ':',
        trackNameCombo, {
            xtype: 'tbseparator', width: 5
        }, _Alarm_FromTime + ':', fromDateEditor, {
            xtype: 'tbseparator', width: 5
        }, fromTimeEditor, {
            xtype: 'tbseparator', width: 5
        }, _Alarm_ToTime + ':', toDateEditor, {
            xtype: 'tbseparator', width: 5
        }, toTimeEditor, {
            xtype: 'tbseparator', width: 5
        }, _SMSResource_InOutType + ':', inOutSMSCombobox, {
            xtype: 'tbseparator', width: 5
        }, {
            tooltip: { text: _TrackerMain_Search, autoHide: true },
            icon: 'images/search16.gif',
            handler: function() {
                var ids = "";
                for (var i = 0; i < trackerstore.getCount(); i++) {
                    var record = trackerstore.getAt(i);
                    if (ids == "")
                        ids = record.get('snid');
                    else
                        ids += "," + record.get('snid');
                }
                ids = trackNameCombo.getValue() == "" ? ids : trackNameCombo.getValue();
                smsReportStore.reload({
                    params: {
                        ids: ids,
                        fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
                        totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
                        start: 0,
                        limit: pagesize,
                        inout: inOutSMSCombobox.getValue()
                    }
                });
            }
        }, {
            tooltip: { text: _TrackerMain_ExportToExcel, autoHide: true },
            icon: 'images/excel16.gif',
            handler: function() {
                if (!Ext.fly('smsexcel')) {
                    var frm = document.createElement('form');
                    frm.id = 'smsexcel';
                    frm.name = id;
                    frm.style.display = 'none';
                    document.body.appendChild(frm);
                }
                var ids = "";
                for (var i = 0; i < trackerstore.getCount(); i++) {
                    var record = trackerstore.getAt(i);
                    if (ids == "")
                        ids = record.get('snid');
                    else
                        ids += "," + record.get('snid');
                }
                ids = trackNameCombo.getValue() == "" ? ids : trackNameCombo.getValue();
                Ext.Ajax.request({
                    url: 'HandlerExcel',
                    form: Ext.fly('smsexcel'),
                    method: 'POST',
                    params: {
                        exceltype: 'smsexcel',
                        ids: ids,
                        fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
                        totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
                        inout: inOutSMSCombobox.getValue(),
                        start: 0,
                        limit: 60000
                    },
                    isUpload: true
                });
            }
        }
]
    });

    var smsReportStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchAllSMS",
            method: "POST",
            timeout: 120000
        }),
        reader: new Ext.data.JsonReader({
            fields: ['sms_id', 'sn_imei_id', 'r_datetime', 'in_out_type', 'sms_content', 'is_location_data', 'fee', 'is_checked', 'tracker_name'],
            root: "data",
            totalProperty: "totalCount"
        })
    });

    var smsReportGrid = new Ext.grid.GridPanel({
        store: smsReportStore,
        loadMask: true,
        columns: [{
            header: _Alarm_Header_Name, width: 120, sortable: true, menuDisabled: true, dataIndex: 'tracker_name'
        }, {
            header: _Alarm_Header_RecTime, width: 150, sortable: true, menuDisabled: true, dataIndex: 'r_datetime'
        }, {
            header: _SMSResource_InOutType, width: 90, sortable: true, menuDisabled: true, dataIndex: 'in_out_type', renderer: function(value) { return value ? _SMSResource_Received : _SMSResource_Send; }
        }, {
            header: _SMSResource_SMSContent, width: 280, sortable: true, menuDisabled: true, dataIndex: 'sms_content'
        }, {
            header: _SMSResource_IsLocationSMS, width: 80, sortable: true, menuDisabled: true, dataIndex: 'is_location_data', renderer: function(value) { return value ? _TrackerMain_Yes : _TrackerMain_No; }
        }, {
            header: _SMSResource_IsChecked, width: 60, hidden:true, sortable: true, menuDisabled: true, dataIndex: 'is_checked', renderer: function(value) { return value ? _TrackerMain_Yes : _TrackerMain_No; }
        }
],
        width: 770,
        height: 420
    });

    smsReportGrid.on('rowdblclick', function(grid, index) {
        var record = grid.getStore().getAt(index);
        if (record.get('is_location_data') == false)
            return;
        Ext.Ajax.request({
            url: 'HandlerGetOneSmsLocation',
            method: 'post',
            params: {
                gpstime: record.get('r_datetime'),
                sn_imei_id: record.get('sn_imei_id')
            },
            success: function(response) {
                var val = Ext.decode(response.responseText);
                if (val != "") {
                    var win = createHistoryMapWindow('(' + record.get('tracker_name') + ',' + record.get('r_datetime') + ')');
                    win.show();
                    nextwindowx += 20;
                    nextwindowy += 20;
                    var rmap = createMap(win.getMap(), true, null, gmapurl); //new MSGMap(win.getMap(), true, null, gmapurl);
                    var myRecord = new Ext.data.Record(val);
                    rmap.drawHistoryPoint(myRecord);
                }
            }
        });
    });


    var smsReportWindow = new Ext.Window({
        layout: 'fit',
        height: 490,
        width: 950,
        items: smsReportGrid,
        tbar: tbar,
        iconCls: 'icon-sms',
        minimizable: true,
        constrain:true,
        bbar: new Ext.PagingToolbar({
            pageSize: pagesize,
            store: smsReportStore,
            beforePageText: _Alarm_Page,
            refreshTex: _Alarm_Refresh,
            displayMsg: _Alarm_Displaying,
            emptyMsg: _Alarm_Alarm_emptyMsg,
            displayInfo: true
        }),
        listeners: {
            'minimize': function() {
                addTaskButtonForWindow(this);
            },
            'show': function() {
                inOutSMSCombobox.setValue(isLoadOneDayMessage == true ? -1 : 1);
            }
        }
    });

    smsReportStore.on('beforeload', function(store) {
        var ids = "";
        for (var i = 0; i < trackerstore.getCount(); i++) {
            var record = trackerstore.getAt(i);
            if (ids == "")
                ids = record.get('snid');
            else
                ids += "," + record.get('snid');
        }
        ids = trackNameCombo.getValue() == "" ? ids : trackNameCombo.getValue();
        smsReportStore.baseParams = {
            start: 'start',
            limit: 'limit',
            ids: ids,
            fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
            totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
            inout: inOutSMSCombobox.getValue()
        }
    });

    
    smsReportWindow.show();
}